package com.ccjq.spring.version.two.webmvc.controller;

import com.ccjq.spring.version.two.webmvc.domain.User;
import com.ccjq.spring.version.two.webmvc.service.UserService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * describe: 用户业务控制器
 *
 * @author xugang
 * @date 2019/1/15 14:02
 */
public class UserController extends MultiActionController {
    private static final Log logger = LogFactory.getLog(UserController.class);

    private UserService userService;

    /**
     * Hello World练习
     *
     * @param request
     * @param response
     * @return
     */
    public ModelAndView hello(HttpServletRequest request, HttpServletResponse response) {
        logger.info("####-------------->>> UserController.hello");
        return new ModelAndView("hello/hello", "message", "Hello World!");
    }

    /**
     * 跳转到注册页面
     *
     * @param request
     * @param response
     * @return
     */
    public ModelAndView register(HttpServletRequest request, HttpServletResponse response) {
        logger.info("####-------------->>> UserController.register");
        return new ModelAndView("user/register", "message", "进入注册页面");
    }

    /**
     * 执行注册
     *
     * @param request
     * @param response
     * @return
     */
    public ModelAndView create(HttpServletRequest request, HttpServletResponse response) {
        logger.info("####-------------->>> UserController.create");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String nickname = request.getParameter("nickname");
        User user = new User(username, password, nickname);
        userService.saveUser(user);
        return new ModelAndView("user/createSuccess", "user", user);
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }
}
